<?php
	define("RES_OK", "OK");
	define("RES_ERR", "ERR");

	// **************************************************************
	// Widget Framework error code (1XX)
	define("WF_ERRCODE_ERR", 100);
	define("WF_ERRCODE_ERR_MSG", "Widget framework internal error");

	define("WF_ERRCODE_INVALIDFORMAT", 101);
	define("WF_ERRCODE_INVALIDFORMAT_MSG", "Invalid protocol data");

	define("WF_ERRCODE_NOACT", 102);
	define("WF_ERRCODE_NOACT_MSG", "Parameter [act] is not specified");

	define("WF_ERRCODE_ACTERR", 103);
	define("WF_ERRCODE_ACTERR_MSG", "Parameter [act] is invalid");

	define("WF_ERRCODE_DBINIT", 104);
	define("WF_ERRCODE_DBINIT_MSG", "Connecting to database failed");

	define("WF_ERRCODE_LACKINPUT", 105);
	define("WF_ERRCODE_LACKINPUT_MSG", "Incorrect parameters");

	define("WF_ERRCODE_INVALIDINPUT", 106);
	define("WF_ERRCODE_INVALIDINPUT_MSG", "Invalid parameters");

	define("WF_ERRCODE_DBERR", 107);
	define("WF_ERRCODE_DBERR_MSG", "Database error");
	
	define("WP_ERRCODE_HTTP_REQUEST_FAIL",			900);
	define("WP_ERRCODE_HTTP_REQUEST_FAIL_MSG",			"Http Request exec failed.");
	
	define("WF_OK_MODULE_SAVED", "module saved");
	define("WF_OK_MODULE_DELETED", "module deleted");
	define("WF_OK_MODULE_ADDED", "module added");
	define("WF_OK_MODULE_TAB_REMOVED", "tab removed");
	define("WF_OK_MODULE_UDATA_SAVED", "udata saved");
	define("WF_OK_MODULE_PDATA_SAVED", "pdata saved");
	define("WF_OK_MODULE_THEME_SAVED", "theme saved");
	define("WF_OK_MODULE_LANG_SAVED", "lang saved");	

	// **************************************************************
	// server list error code (2XX)
	define("SLIST_ERRCODE_WFERR", 200);
	define("SLIST_ERRCODE_WFERR_MSG", "Widget framework internal error");

	define("SLIST_ERRCODE_NOACT", 201);
	define("SLIST_ERRCODE_NOACT_MSG", "Parameter [act] is not specified");

	define("SLIST_ERRCODE_LACKINPUT", 202);
	define("SLIST_ERRCODE_LACKINPUT_MSG", "Incorrect parameters");

	define("SLIST_ERRCODE_CMDERR", 203);
	define("SLIST_ERRCODE_CMDERR_MSG", "Invalid command");

	define("SLIST_ERRCODE_INVALIDFORMAT", 204);
	define("SLIST_ERRCODE_INVALIDFORMAT_MSG", "Invalid format");

	define("SLIST_ERRCODE_DBINIT", 205);
	define("SLIST_ERRCODE_DBINIT_MSG", "Connecting to database failed");

	define("SLIST_ERRCODE_DBERR", 206);
	define("SLIST_ERRCODE_DBERR_MSG", "Database error");

	// server list successful msg
	define("SLIST_OK_ADD_MSG", "Server information saved");
	define("SLIST_OK_DEL_MSG", "Server deleted");
	define("SLIST_OK_UPDATE_MSG", "Server information updated");

	// **************************************************************
	// proxy list error code (3XX)
	define("PLIST_ERRCODE_WFERR", 300);
	define("PLIST_ERRCODE_WFERR_MSG", "Widget framework internal error");

	define("PLIST_ERRCODE_NOACT", 301);
	define("PLIST_ERRCODE_NOACT_MSG", "Parameter [act] is not specified");

	define("PLIST_ERRCODE_LACKINPUT", 302);
	define("PLIST_ERRCODE_LACKINPUT_MSG", "Incorrect parameters");

	define("PLIST_ERRCODE_CMDERR", 303);
	define("PLIST_ERRCODE_CMDERR_MSG", "Invalid command");

	define("PLIST_ERRCODE_INVALIDFORMAT", 304);
	define("PLIST_ERRCODE_INVALIDFORMAT_MSG", "Invalid format");

	define("PLIST_ERRCODE_DBINIT", 305);
	define("PLIST_ERRCODE_DBINIT_MSG", "Connecting to database failed");

	define("PLIST_ERRCODE_DBERR", 306);
	define("PLIST_ERRCODE_DBERR_MSG", "Database error");

	// proxy list successful msg
	define("PLIST_OK_ADD_MSG", "Proxy information saved");
	define("PLIST_OK_DEL_MSG", "Proxy deleted");
	define("PLIST_OK_UPDATE_MSG", "Proxy information updated");

	// **************************************************************
	// proxy_controller error code (4XX)
	require_once(dirname(__FILE__)."/../inc/proxy_errorcode.php");

	// **************************************************************
	// proxy enable error code (5XX)
	define("PENABLE_ERRCODE_MODULEERR", 500);
	define("PENABLE_ERRCODE_MODULEERR_MSG", "Proxy Enable module failed");

	define("PENABLE_ERRCODE_LACKINPUT", 501);
	define("PENABLE_ERRCODE_LACKINPUT_MSG", "Incorrect input");

	define("PENABLE_ERRCODE_WFERR", 502);
	define("PENABLE_ERRCODE_WFERR_MSG", "Widget framework internal error");

	define("PENABLE_ERRCODE_DBINIT", 503);
	define("PENABLE_ERRCODE_DBINIT_MSG", "Connecting to database failed");

	define("PENABLE_ERRCODE_DBERR", 504);
	define("PENABLE_ERRCODE_DBERR_MSG", "Database error");

	define("PENABLE_ERRCODE_INVALIDFORMAT", 505);
	define("PENABLE_ERRCODE_INVALIDFORMAT_MSG", "Invalid format");

	// proxy enable successful msg
	define("PENABLE_OK_ENABLE_MSG", "proxy enabled");
	define("PENABLE_OK_DISABLE_MSG", "proxy disabled");

	// **************************************************************
	// login/signup error code (6XX)
	define("LOGIN_ERRCODE_LACKINPUT", 601);
	define("LOGIN_ERRCODE_LACKINPUT_MSG", "Incorrect input");
	define("LOGIN_ERRCODE_DBERR", 602);
	define("LOGIN_ERRCODE_DBERR_MSG", "Database error");
	define("LOGIN_ERRCODE_LOGINFAIL", 603);
	define("LOGIN_ERRCODE_LOGINFAIL_MSG", "Incorrect email or password");
	define("LOGIN_ERRCODE_NOUID", 604);
	define("LOGIN_ERRCODE_NOUID_MSG", "[uid] is not specified");
	define("LOGIN_ERRCODE_SHORTPASSWORD", 605);
	define("LOGIN_ERRCODE_SHORTPASSWORD_MSG", "Password is too short (<6)");
	define("LOGIN_ERRCODE_EMAILFMT", 606);
	define("LOGIN_ERRCODE_EMAILFMT_MSG", "Invalid email format");
	define("LOGIN_ERRCODE_EMAILDUP", 607);
	define("LOGIN_ERRCODE_EMAILDUP_MSG", "Email already exists");

	define("LOGIN_OK_SUCCESS_MSG", "login successfully");
	define("LOGIN_OK_EMAILSENT_MSG", "email sent");
	define("LOGIN_OK_REGISTER_MSG", "register successfully");
	
	// **************************************************************
	// Widget Package Manager/Widget Pool (7XX)
	define("WPM_ERRCODE_WFERR", 700);
	define("WPM_ERRCODE_WFERR_MSG", "Widget framework internal error");

	define("WPM_ERRCODE_NOACT", 701);
	define("WPM_ERRCODE_NOACT_MSG", "Parameter [act] is not specified");

	define("WPM_ERRCODE_INITERR", 702);
	define("WPM_ERRCODE_INITERR_MSG", "Widget pool related information is missing");

	define("WPM_ERRCODE_INVALIDFORMAT", 703);
	define("WPM_ERRCODE_INVALIDFORMAT_MSG", "Invalid format");

	define("WPM_ERRCODE_OLD_SUMMARY_NOT_EXIST",			704);
	define("WPM_ERRCODE_OLD_SUMMARY_NOT_EXIST_MSG",	"Old WidgetSummary.xml doesn't exist");
	
	define("WPM_ERRCODE_OLD_SUMMARY_LOAD_FAIL",				705);
	define("WPM_ERRCODE_OLD_SUMMARY_LOAD_FAIL_MSG",	"Load old WidgetSummary.xml fail");
	
	define("WPM_ERRCODE_NEW_SUMMARY_NOT_EXIST",		706);
	define("WPM_ERRCODE_NEW_SUMMARY_NOT_EXIST_MSG",		"New WidgetSummary.xml doesn't exist");
	
	define("WPM_ERRCODE_NEW_SUMMARY_LOAD_FAIL",		707);
	define("WPM_ERRCODE_NEW_SUMMARY_LOAD_FAIL_MSG",	"Load new WidgetSummary.xml fail");
	
	define("WPM_ERRCODE_LANG_XML_LOAD_FAIL",	708);
	define("WPM_ERRCODE_LANG_XML_LOAD_FAIL_MSG",	"Load lanaguage xml file fail");
	
	define("WPM_ERRCODE_UPD_WIDGETLIST",			709);
	define("WPM_ERRCODE_UPD_WIDGETLIST_MSG",			"Update widgetList.js fail");
	
	define("WPM_NO_NEED_UPDATE", "No need to update");
	define("WPM_NEED_UPDATE", "Need update");
	define("WPM_UPDATE_SUCCESSFULLY", "Update successfully");
	define("WPM_ANOTHER_PROCESS_IS_RUNNING", "Another process is running");
	
?>
<?php
	require_once(dirname(__FILE__)."/../inc/debug.php");
?>
<?php

function result_output($obj) {
	mydebug_log("WF Response - ".json_encode($obj));
	echo json_encode($obj);
	return;
}

function make_error_response( $code, $msg) {
	$response = array();
	$response["response"] = RES_ERR;
	$response["errcode"] = $code;
	$response["timestamp"] = time();
	$response["message"] = $msg;
	result_output($response);
	return;
}

function make_successful_response( $msg, $robj) {
	$response = array();
	$response["response"] = RES_OK;
	$response["errcode"] = 0;
	$response["timestamp"] = time();
	if( $msg != null ) {
		$response["message"] = $msg;
	}
	if( $robj != null ) {
		$response["result"] = $robj;
	} else {
		$response["result"] = "";
	}
	result_output($response);
	return;
}

?>
<?php
	// json output
	header("Content-Type: application/json");
?>
